<template>
    <div>

        <el-button @click="addUser()"  type="primary" size="small" style="margin-left: 100px">增加用户</el-button>
        <el-table
                class="el-table"
                :data="userList"
                border
                style="width: 70.1%;margin-top:12px;margin-left: 100px"
        >
            <el-table-column

                    prop="id"
                    label="用户id"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="昵称"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="用户名"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="email"
                    label="邮箱"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="角色（0用户，1管理员）"
                    width="200">
            </el-table-column>
            <el-table-column

                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-button  type="primary" size="small" style="margin-left: 10px" @click="deleteUser(scope.row)">删除</el-button>

                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
    export default {
        name: "Controller",
        data(){
            return {
                isshow: false,
                uploadParm: {}, //图片的上传
                ruleForm: {},//修改密码的表单
                activeName: "first",
                loading: true,
                baseUrl: '/avatar/',
                user: {},//用户信息表单
                formLabelWidth: "150px",
                username: "",
                role: 0,
                imageUrl: '',
                avatar: '',
                avatar1: "http://localhost:8081/user/uploadAvatar",
                token: localStorage.getItem("token"),
                userList: []
            }
        },
        created() {
            this.getUser();


        },
        methods: {
            deleteUser(user){
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.get('/user/deleteUser?id='+user.id).then(res=>{
                        console.log(res.data)
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });

                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            addUser(){
                this.$router.push('/register');
            },

            getUser() {

                if(this.$store.getters.getUser.username){

                    this.username = this.$store.getters.getUser.username
                }

                this.$axios.get('/user/getUserDetail?username='+this.username,{
                    headers: {
                        "Authorization": localStorage.getItem("token")
                    }
                }).then(res => {


                    this.user = res.data.data;
                    if(this.user.status==1){

                        this.getUserList();
                    }else{
                        this.role=0;
                    }



                });
            },

            getUserList(){
                this.$axios.get('/user/getUserList').then(res=>{
                    this.userList=res.data.data;
                })
            }
        }
    }
</script>

<style scoped>

</style>